# Maintainer: Saul Ibarra Corretge <saghul@gmail.com>
# Contributor: Alexey Pavlov <alexpux@gmail.com>
# Contributor: Mehdi Chinoune <mehdi.chinoune@hotmail.com>

_realname=sip
pkgbase=mingw-w64-${_realname}
pkgname=("${MINGW_PACKAGE_PREFIX}-${_realname}")
conflicts=("${MINGW_PACKAGE_PREFIX}-sip4")
replaces=("${MINGW_PACKAGE_PREFIX}-sip5")
pkgver=6.7.11
pkgrel=1
pkgdesc="A tool that makes it easy to create Python bindings for C and C++ libraries (mingw-w64)"
arch=('any')
mingw_arch=('mingw32' 'mingw64' 'ucrt64' 'clang64' 'clang32' 'clangarm64')
license=('custom:"sip"')
url="https://riverbankcomputing.com/software/sip"
depends=("${MINGW_PACKAGE_PREFIX}-python"
         "${MINGW_PACKAGE_PREFIX}-python-ply"
         "${MINGW_PACKAGE_PREFIX}-python-packaging"
         "${MINGW_PACKAGE_PREFIX}-python-setuptools")
makedepends=("${MINGW_PACKAGE_PREFIX}-python-build"
             "${MINGW_PACKAGE_PREFIX}-python-installer"
             "${MINGW_PACKAGE_PREFIX}-python-wheel"
             "${MINGW_PACKAGE_PREFIX}-cc")
options=(!strip)
source=(https://pypi.python.org/packages/source/s/sip/sip-${pkgver}.tar.gz
        001-mingw-python.patch
        002-wrappers.patch)
sha256sums=('f0dc3287a0b172e5664931c87847750d47e4fdcda4fe362b514af8edd655b469'
            '15d64d0ff5253096a3fb5d3d4584eab8f93b9e9ffbcfee1c02dce00d695e6027'
            'ba695df87296ad5be7a64d57ce0a8f54db3867a9b69b303b76482f2ac48bb7d6')

prepare() {
  cd "${srcdir}"/${_realname}-${pkgver}

  # Apply patches
  patch -p1 -i "${srcdir}"/001-mingw-python.patch
  patch -p1 -i "${srcdir}"/002-wrappers.patch

  cd "${srcdir}"
  rm -rf python-build-${MSYSTEM} | true
  cp -r "${_realname}-${pkgver}" "python-build-${MSYSTEM}"
}

build() {
  cd python-build-${MSYSTEM}

  ${MINGW_PREFIX}/bin/python -m build --wheel --skip-dependency-check --no-isolation
}

package() {
  cd "${srcdir}"/python-build-${MSYSTEM}
  MSYS2_ARG_CONV_EXCL="--prefix=" \
    ${MINGW_PREFIX}/bin/python -m installer --prefix=${MINGW_PREFIX} \
    --destdir="${pkgdir}" dist/*.whl

  install -Dm644 LICENSE "${pkgdir}${MINGW_PREFIX}/share/licenses/${_realname}/LICENSE"
  install -Dm644 LICENSE-GPL2 "${pkgdir}${MINGW_PREFIX}/share/licenses/${_realname}/LICENSE-GPL2"
  install -Dm644 LICENSE-GPL3 "${pkgdir}${MINGW_PREFIX}/share/licenses/${_realname}/LICENSE-GPL3"
}
